from common.sqlalchemy_package import *
from model.total import *
from flask import g


class Order(BaseModel):
    __tablename__ = 'order'
    order_id = mapped_column(Integer, primary_key=True)  # 订单id
    car_number = mapped_column(String)  # 车牌
    order_number = mapped_column(Integer)  # 订单编号
    order_type=mapped_column(String)     #订单类型备注
    money_sub_time=mapped_column(DateTime) #订单提交时间
    money=mapped_column(Integer)          #金额

    # 分页获取订单信息
    @staticmethod
    def query_order_page_all(page):
        per_page = 8  # 每页返回的数据量
        start_index = (int(page) - 1) * per_page  # 计算每页数据的起始索引
        query = g.db_session.query(Order)
        # 查询满足条件的记录总数
        total_count = query.count()
        # 计算总页数
        total_pages = (total_count + per_page - 1) // per_page
        # 分页查询并逆向排序
        results = query.order_by(Order.order_id.desc()).limit(per_page).offset(start_index).all()
        return results, total_pages

    # 根据订单id查询信息
    @staticmethod
    def query_order_first(order_id):
        query = g.db_session.query(Order).filter(Order.order_id == int(order_id)).first()
        return query
    # 根据车牌号模糊查询订单信息
    @staticmethod
    def order_car_number_search(car_number):
        return g.db_session.query(Order).filter(Order.car_number.like('%' + car_number + '%')).order_by(
            Order.order_id.desc()).all()

    # 查询最后一条订单记录id
    @staticmethod
    def query_end_order():
        query = g.db_session.query(Order).order_by(Order.order_id.desc()).first()#倒叙查询第一条
        if query is None:
            relust=0
        else:
            relust=query.order_id
        return relust

    #添加订单
    @staticmethod
    def add_order(car_number,order_number,order_type,money_sub_time,money):
        try:
            order=Order(car_number=car_number,order_number=int(order_number),order_type=order_type
                        ,money_sub_time=money_sub_time,money=money)
            g.db_session.add(order)
            g.db_session.commit()
            return 0
        except Exception as e:
            print(e)
            return -1

    #删除订单
    @staticmethod
    def del_order(order_id):
        try:
            query = g.db_session.query(Order).filter(Order.order_id==int(order_id)).first()  # 根据订单id查询
            g.db_session.delete(query)
            g.db_session.commit()
            return 0
        except:
            return -1

    #根据车牌号查询订单信息
    @staticmethod
    def query_car_number_order(page,num,car_number):
        # 分页获取订单信息
        per_page = int(num)  # 每页返回的数据量
        start_index = (int(page) - 1) * per_page  # 计算每页数据的起始索引
        query = g.db_session.query(Order).filter(Order.car_number==car_number)
        # 查询满足条件的记录总数
        total_count = query.count()
        # 计算总页数
        total_pages = (total_count + per_page - 1) // per_page
        # 分页查询并逆向排序
        results = query.order_by(Order.order_id.desc()).limit(per_page).offset(start_index).all()
        return results, total_pages

    # 根据订单编号模糊查询,该车辆信息
    @staticmethod
    def order_number_search(car_number,order_number):
        return g.db_session.query(Order).filter(Order.car_number==car_number,
                                                Order.order_number.like('%' + order_number + '%')).order_by(
            Order.order_id.desc()).all()